package com.lastproject.ejb.mdbs;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import com.lastproject.ejb.service.MailSender;

/**
 * Message-Driven Bean implementation class for: FirstMDB
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "jms/firstResource")
public class FirstMDB implements MessageListener {

	public FirstMDB() {

	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {
		String emails = "";
		String info = "";
		String subject = "";

		try {
			emails = message.getStringProperty("emails");
			info = message.getStringProperty("surveyInfo");
			subject = message.getStringProperty("subject");
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String[] emailToSend;
		emailToSend = emails.split("_");
		System.out.println("1: " + emailToSend[0]);

		for (int i = 0; i < Integer.parseInt(emailToSend[0]); i++) {
			MailSender mail = new MailSender();
			try {
				mail.sendMail(emailToSend[i+1], subject, info);

			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

}
